package com.shop.loan.proxy;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.shop.loan.mapper.LoanOrderBillByMybatisMapper;
import com.shop.loan.model.entity.LoanOrderBillByMybatis;
import com.shop.loan.model.entity.LoanOrderByMybatis;
import com.shop.loan.model.view.StatisticsByRepaymentDay;
import com.shop.loan.util.Util;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.erupt.annotation.fun.DataProxy;
import xyz.erupt.annotation.query.Condition;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsByRepaymentDayProxy implements DataProxy<StatisticsByRepaymentDay> {

    @Resource
    LoanOrderBillByMybatisMapper loanOrderBillByMybatisMapper;

    @Resource
    Util util;


    @Override
    public String beforeFetch(List<Condition> conditions) {
        conditions.removeIf(condition -> "channel".trim().equals(condition.getKey()));
        return null;
    }

    @Override
    public void afterFetch(Collection<Map<String, Object>> list) {
        String channelIdStr = util.getSearchParam("channel", 1);
        Long channelId = StringUtils.hasLength(channelIdStr) ? Long.valueOf(channelIdStr) : null;
        for (Map<String, Object> stringObjectMap : list) {
            String dayStr = stringObjectMap.get("day").toString();
            DateTime dayTime = DateUtil.parse(dayStr);
            String timeStartStr = DateUtil.beginOfDay(dayTime).toString();
            String timeEndStr = DateUtil.endOfDay(dayTime).toString();

            LoanOrderByMybatis loanOrderByMybatis = new LoanOrderByMybatis();
            loanOrderByMybatis.setStartTime(timeStartStr);
            loanOrderByMybatis.setEndTime(timeEndStr);
            loanOrderByMybatis.setChannelId(channelId);
            loanOrderByMybatis.setUserType(Integer.parseInt(stringObjectMap.get("type").toString()));

            Integer orderNumByTotal = loanOrderBillByMybatisMapper.selectCount(loanOrderByMybatis);
            BigDecimal totalMoney = BigDecimal.ZERO, principalByTotal = BigDecimal.ZERO,
                    repayMoneyByTotal = BigDecimal.ZERO, repayMoneyByPart = BigDecimal.ZERO, repayMoneyBySettle = BigDecimal.ZERO;
            LoanOrderBillByMybatis billSum = loanOrderBillByMybatisMapper.selectSum(loanOrderByMybatis);
            if (billSum != null) {
                totalMoney = billSum.getMoney().add(billSum.getRateMoney()).add(billSum.getShopMoney()).add(billSum.getOverdueFine());
                principalByTotal = billSum.getMoney();
                repayMoneyByTotal = billSum.getPayMoney();
            }

            loanOrderByMybatis.setOverdueStatus(false);
            Integer orderNumByOverdue = loanOrderBillByMybatisMapper.selectCount(loanOrderByMybatis);
            billSum = loanOrderBillByMybatisMapper.selectSum(loanOrderByMybatis);
            BigDecimal overdueMoney = BigDecimal.ZERO;
            if (billSum != null) {
                overdueMoney = billSum.getMoney().add(billSum.getRateMoney()).add(billSum.getShopMoney()).subtract(billSum.getPayMoney());
            }
            BigDecimal overdueRate = overdueMoney.divide(totalMoney.compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ONE : totalMoney, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));

            loanOrderByMybatis.setOverdueStatus(null);
            loanOrderByMybatis.setIsPart(2);
            Integer orderNumByPart = loanOrderBillByMybatisMapper.selectCount(loanOrderByMybatis);
            billSum = loanOrderBillByMybatisMapper.selectSum(loanOrderByMybatis);
            if (billSum != null) {
                repayMoneyByPart = billSum.getPayMoney();
            }

            loanOrderByMybatis.setIsPart(null);
            loanOrderByMybatis.setPayStatus(1);
            Integer orderNumBySettle = loanOrderBillByMybatisMapper.selectCount(loanOrderByMybatis);
            BigDecimal orderNumRateByRepay = BigDecimal.valueOf(orderNumBySettle).divide(BigDecimal.valueOf(orderNumByTotal == 0 ? 1 : orderNumByTotal), RoundingMode.DOWN);
            billSum = loanOrderBillByMybatisMapper.selectSum(loanOrderByMybatis);
            if (billSum != null) {
                repayMoneyBySettle = billSum.getPayMoney();
            }

            BigDecimal repayRate = repayMoneyByTotal.divide(totalMoney.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : totalMoney, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));
            BigDecimal profit = repayMoneyByTotal.subtract(principalByTotal);
            BigDecimal principalRate = repayMoneyByTotal.divide(principalByTotal.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : principalByTotal, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));

            stringObjectMap.put("orderNumByTotal", orderNumByTotal == null ? 0 : orderNumByTotal);
            stringObjectMap.put("totalMoney", totalMoney == null ? 0 : totalMoney);
            stringObjectMap.put("principalByTotal", principalByTotal == null ? 0 : principalByTotal);
            stringObjectMap.put("repayMoneyByTotal", repayMoneyByTotal == null ? 0 : repayMoneyByTotal);
            stringObjectMap.put("repayMoneyByPart", repayMoneyByPart == null ? 0 : repayMoneyByPart);
            stringObjectMap.put("orderNumByOverdue", orderNumByOverdue == null ? 0 : orderNumByOverdue);
            stringObjectMap.put("overdueMoney", overdueMoney == null ? 0 : overdueMoney);
            stringObjectMap.put("overdueRate", overdueRate == null ? 0 : overdueRate);
            stringObjectMap.put("repayMoneyBySettle", repayMoneyBySettle == null ? 0 : repayMoneyBySettle);
            stringObjectMap.put("orderNumByPart", orderNumByPart == null ? 0 : orderNumByPart);
            stringObjectMap.put("orderNumBySettle", orderNumBySettle == null ? 0 : orderNumBySettle);
            stringObjectMap.put("orderNumRateByRepay", orderNumRateByRepay == null ? 0 : orderNumRateByRepay);
            stringObjectMap.put("repayRate", repayRate == null ? 0 : repayRate);
            stringObjectMap.put("profit", profit == null ? 0 : profit);
            stringObjectMap.put("principalRate", principalRate == null ? 0 : principalRate);
        }
    }


}
